
package com.geek.javase.ch10.jformdesigntest.japanel;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LoginPanel extends JPanel {
    private UserService userService = new UserService();
    private MyFrame my;

    public LoginPanel(MyFrame my) {
        this.my = my;
        initComponents();
    }
    //点击登录 跳转至注册界面
    private void registerButtonActionPerformed(ActionEvent e) {
        // TODO add your code here
        //获取当前父级容器的布局管理器———>CardLayout
        CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
        cardLayout.show(this.getParent(),"register");
        //清空当前界面
        clearLogin();
    }
    //点击登录 跳转至主界面
    private void loginButtonActionPerformed(ActionEvent e) {
        // TODO add your code here
        //收集表单数据
        String username = nameTextField.getText();
        String password = new String(passwordField.getPassword());
        if(username != null && password != null &&
                !username.equals("")  && !password.equals("")) {
            User user = userService.login(username, password);

            if (user != null) {
                JOptionPane.showMessageDialog(null, "登录成功！");
                //渲染主界面数据
                my.user = user;
                my.mainPanel.initMain();

                //跳转界面
                //获取当前父级容器的布局管理器———>CardLayout
                CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
                cardLayout.show(this.getParent(), "main");

                //清空当前登录界面
                clearLogin();
            }else{
                //清空密码
                passwordField.setText("");
            }
        }
    }
    //清空登录页面数据
    public void clearLogin(){
        nameTextField.setText("");
        passwordField.setText("");
    }
    //初始化用户姓名
    public void initLogin(){
        nameTextField.setText(my.user.getName());
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        titleLabel = new JLabel();
        nameLabel = new JLabel();
        nameTextField = new JTextField();
        pwdLabel = new JLabel();
        registerButton = new JButton();
        loginButton = new JButton();
        passwordField = new JPasswordField();

        //======== this ========
        setLayout(null);

        //---- titleLabel ----
        titleLabel.setText("\u767b \u5f55 \u754c \u9762");
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
        add(titleLabel);
        titleLabel.setBounds(115, 5, 140, 67);

        //---- nameLabel ----
        nameLabel.setText("\u59d3 \u540d");
        nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        nameLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        add(nameLabel);
        nameLabel.setBounds(50, 95, 32, 19);
        add(nameTextField);
        nameTextField.setBounds(105, 90, 180, 30);

        //---- pwdLabel ----
        pwdLabel.setText("\u5bc6 \u7801");
        pwdLabel.setHorizontalAlignment(SwingConstants.CENTER);
        pwdLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        add(pwdLabel);
        pwdLabel.setBounds(50, 150, 32, 19);

        //---- registerButton ----
        registerButton.setText("\u6ce8\u518c");
        registerButton.addActionListener(e -> registerButtonActionPerformed(e));
        add(registerButton);
        registerButton.setBounds(225, 225, 78, 30);

        //---- loginButton ----
        loginButton.setText("\u767b\u5f55");
        loginButton.addActionListener(e -> loginButtonActionPerformed(e));
        add(loginButton);
        loginButton.setBounds(95, 225, 78, 30);
        add(passwordField);
        passwordField.setBounds(105, 145, 180, 30);

        setPreferredSize(new Dimension(400, 300));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel titleLabel;
    private JLabel nameLabel;
    private JTextField nameTextField;
    private JLabel pwdLabel;
    private JButton registerButton;
    private JButton loginButton;
    private JPasswordField passwordField;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
